package com.dazhao.animejaphome.home;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dazhao.animejaphome.R;
import com.dazhao.animejaphome.adapter.NewsAdapter;
import com.dazhao.animejaphome.bean.TitlesBean;
import com.dazhao.animejaphome.httpservice.TitlesAPI;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Jay on 2015/8/28 0028.
 */
public class MyFragment2News extends Fragment {
    @BindView(R.id.recycler_view_news)
    RecyclerView recyclerView;

    public MyFragment2News() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news, container, false);
        ButterKnife.bind(this,view);




        Retrofit retrofit= new Retrofit.Builder()
                .baseUrl("http://114.67.131.62/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        TitlesAPI service = retrofit.create(TitlesAPI.class);

        Call<TitlesBean> model = service.repo("AnimeJap/titles.json");//至此创建了一个 get请求 的对象

        model.enqueue(new Callback<TitlesBean>() {//发送请求
            @Override
            public void onResponse(Call<TitlesBean> call, Response<TitlesBean> response) {
                //取出json中titles的值
                String string = response.body().getTitles();
                //分割开ResList
                String[] titles =string.split(",");

                ArrayList<String> titlesList=new ArrayList<String>(Arrays.asList(titles));

                recyclerView.setLayoutManager(new LinearLayoutManager(MyFragment2News.this.getContext(),LinearLayoutManager.VERTICAL,false));
                recyclerView.setAdapter(new NewsAdapter(MyFragment2News.this.getContext(),titlesList));
                recyclerView.addItemDecoration(new DividerItemDecoration(MyFragment2News.this.getContext(),DividerItemDecoration.VERTICAL));
            }

            @Override
            public void onFailure(Call<TitlesBean> call, Throwable t) {

            }
        });


        return view;
    }


}
